package com.taotao.service.impl;

import com.taotao.common.EUTreeNode;
import com.taotao.common.TaotaoResult;
import com.taotao.mapper.TbContentCategoryMapper;
import com.taotao.pojo.TbContentCategory;
import com.taotao.pojo.TbContentCategoryExample;
import com.taotao.service.TbContentCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class TbContentCategoryServiceImpl implements TbContentCategoryService {

    @Autowired
    private TbContentCategoryMapper tbContentCategoryMapper;

    /**
     * 查询所有的节点
     * @param parentId
     * @return
     */
    @Override
    public List<EUTreeNode> TbContentCategory(long parentId) {
        // 按照parentId查询分类数据
        TbContentCategoryExample tbContentCategoryExample = new TbContentCategoryExample();
        tbContentCategoryExample.createCriteria().andParentIdEqualTo(parentId).andStatusNotEqualTo(2);
        List<TbContentCategory> tbContentCategoryList = tbContentCategoryMapper.selectByExample(tbContentCategoryExample);

        List<EUTreeNode> euTreeNodeList = new ArrayList<>();
        // 把查询到的分类数据封装成EasyUI异步树数据结构
        for (TbContentCategory tbContentCategory : tbContentCategoryList){
            EUTreeNode euTreeNode = new EUTreeNode();
            euTreeNode.setId(tbContentCategory.getId());
            euTreeNode.setState(tbContentCategory.getIsParent() ? "closed" : "open");
            euTreeNode.setText(tbContentCategory.getName());
            euTreeNodeList.add(euTreeNode);
        }
        return euTreeNodeList;
    }

    /**
     * 添加一个节点并获取其自增的id
     * @param tbContentCategory
     * @return
     */
    @Override
    public TaotaoResult createTbContentCategory(TbContentCategory tbContentCategory) {
        Date time = new Date();

        tbContentCategory.setIsParent(false);
        tbContentCategory.setCreated(time);
        tbContentCategory.setUpdated(time);
        tbContentCategory.setSortOrder(1);
        tbContentCategoryMapper.insertSelective(tbContentCategory);

        long parentId = tbContentCategory.getParentId();
        TbContentCategory contentCategory = new TbContentCategory();
        contentCategory.setId(parentId);
        contentCategory.setIsParent(true);
        tbContentCategoryMapper.updateByPrimaryKeySelective(contentCategory);

        return new TaotaoResult(tbContentCategory);
    }

    /**
     * 更改节点的name
     * @param tbContentCategory
     * @return
     */
    @Override
    public TaotaoResult updateTbContentCategoryName(TbContentCategory tbContentCategory) {
        tbContentCategory.setUpdated(new Date());
        tbContentCategoryMapper.updateByPrimaryKeySelective(tbContentCategory);

        return TaotaoResult.ok();
    }

    /**
     * 删除节点
     * @param id
     * @return
     */
    @Override
    public TaotaoResult deleteTbContentCategory(Long id) {
        //查询将要删除节点的parentId
        Long parentId = tbContentCategoryMapper.selectByPrimaryKey(id).getParentId();

        //软删除节点(更改status为删除状态)
        TbContentCategory tbContentCategory = new TbContentCategory();
        tbContentCategory.setId(id);
        tbContentCategory.setStatus(2);
        tbContentCategoryMapper.updateByPrimaryKeySelective(tbContentCategory);

        //查询将要删除节点的所有子节点,并判断其是否还有其他子节点
        TbContentCategoryExample tbContentCategoryExample = new TbContentCategoryExample();
        tbContentCategoryExample.createCriteria().andParentIdEqualTo(id).andStatusNotEqualTo(2);
        List<TbContentCategory> contentCategoryList = tbContentCategoryMapper.selectByExample(tbContentCategoryExample);
        if (contentCategoryList == null || contentCategoryList.size() < 1){
            //无子节点,则判断其父节点是否还有其他子节点.若无,则更改其父节点的is_parent字段的值改为0;若有,则不管
            updateParentIsParent(parentId);
        }else {
            //有子节点,则将其所有子节点均进行软删除
            for (TbContentCategory category : contentCategoryList){
                TbContentCategory contentCategory = new TbContentCategory();
                contentCategory.setId(category.getId());
                contentCategory.setStatus(2);
                tbContentCategoryMapper.updateByPrimaryKeySelective(contentCategory);
            }
            //删除之后判断其父节点是否还有其他子节点.若无,则更改其父节点的is_parent字段的值改为0;若有,则不管
            updateParentIsParent(parentId);
        }

        return TaotaoResult.ok();
    }
    private void updateParentIsParent(Long parentId){
        TbContentCategoryExample contentCategoryExample = new TbContentCategoryExample();
        contentCategoryExample.createCriteria().andParentIdEqualTo(parentId).andStatusNotEqualTo(2);
        List<TbContentCategory> tbContentCategoryList = tbContentCategoryMapper.selectByExample(contentCategoryExample);
        if (tbContentCategoryList == null || tbContentCategoryList.size() < 1){
            TbContentCategory category = new TbContentCategory();
            category.setId(parentId);
            category.setIsParent(false);
            tbContentCategoryMapper.updateByPrimaryKeySelective(category);
        }
    }
}
